package com.ce.pms.occupation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.occupation.dao.IIdentityValidateDao;
import com.ce.pms.occupation.dao.IPerfectItemDispatchDao;
import com.ce.pms.occupation.dao.IPerfectItemRequisitionDao;
import com.ce.pms.occupation.model.IdentityValidateDO;
import com.ce.pms.occupation.model.InspecteRequisiteDO;
import com.ce.pms.occupation.model.OccHouseClientInfoDO;
import com.ce.pms.occupation.model.PerfectItemDispatchDO;
import com.ce.pms.occupation.model.PerfectItemRequisitionDO;
import com.ce.pms.occupation.service.IPerfectItemRequisitionService;
import com.ce.pms.occupation.vo.PerfectItemRequisitionVO;
import com.ce.pms.occupation.vo.PerfectItemSearchVO;
import com.ce.pms.system.occupation.model.PerfectItemTypeConfigDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/**
 * 功能描述: 整改项目Service实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-30
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class PerfectItemRequisitionServiceImpl extends PmsBaseServiceImpl implements
		IPerfectItemRequisitionService {
	/**
	 * 方法描述: 分页查询所有整改项目登记
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param SearchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryPerfectItemRequisitionVOByPage(PerfectItemSearchVO searchVO){
		Assert.notNull(searchVO);
		List list = perfectItemDispatchDao.findAll();
		Page page = perfectItemRequisitionDao.queryPerfectItemRequisitionDOByPage(searchVO);
		List<PerfectItemRequisitionVO> VOlist = this.changelistDOToVO(page.getResult());
		page.setResult(VOlist);
		return page;
	}
	/**
	 * 方法描述: 根据ID查询整改项目登记VO
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param id
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionVO queryPerfectItemRequisitionVOById(Long id) {
		Assert.notNull(id,"id isnot Null");
		PerfectItemRequisitionDO itemRequisitionDO = perfectItemRequisitionDao.findById(id);
		return this.assembleD2V(itemRequisitionDO, null);
	}
	/**
	 * 方法描述: 根据派工ID查询所有整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param searchVO
	 * @return List<PerfectItemRequisitionVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemRequisitionVO> queryPerItemListByDispatchId(PerfectItemSearchVO searchVO){
		List<PerfectItemRequisitionDO> DOlist = perfectItemRequisitionDao.queryPerItemListByDispatchId(searchVO);
		return changelistDOToVO(DOlist);
	}
	/**
	 * 方法描述: 根据派工ID查询所有未完工的整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param perfectItemDispatchId
	 * @return List<PerfectItemRequisitionVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemRequisitionVO> queryNoComPerItemSByDispatchId(Long perfectItemDispatchId){
		List<PerfectItemRequisitionDO> DOlist = perfectItemRequisitionDao.getPerItemsByDispatchId(perfectItemDispatchId);
		return changelistDOToVO(DOlist);
	}
	/**
	 * 方法描述: 根据验房ID查询所有整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param inspecteRequisiteId
	 * @return List<PerfectItemRequisitionVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemRequisitionVO> queryPerItemListByInspReqId(Long inspecteRequisiteId){
		List<PerfectItemRequisitionDO> DOlist = perfectItemRequisitionDao.queryPerItemListByInspReqId(inspecteRequisiteId);
		return changelistDOToVO(DOlist);
	}
	/**
	 * 方法描述: 增加整改项目登记方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemRequisitionVO
	 * @return PerfectItemRequisitionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionVO addPerfectItemRequisition(PerfectItemRequisitionVO perfectItemRequisitionVO){
		Assert.notNull(perfectItemRequisitionVO);
		Assert.notNull(perfectItemRequisitionVO.getInspectionRequisitionId(),"验房对象ID不能为空");
		Assert.notNull(perfectItemRequisitionVO.getPerfectItemTypeConfigId(),"整改项目类型ID不能为空");
		this.checkAddIsTrue(perfectItemRequisitionVO.getInspectionRequisitionId());
		if(SysrefcodeDO.OCCUPATION_IN_MANAGEHOUSE.equals(perfectItemRequisitionVO.getJoinMode())){
			IdentityValidateDO validateDO = identityValidateDao.findUniqueByHouseId(perfectItemRequisitionVO.getHouseId());
			if(!BlankUtil.isBlank(validateDO)){
				StringBuffer houseNumber = new StringBuffer("");
				Set<OccHouseClientInfoDO> clientInfos = validateDO.getClientInfos();
				if(!BlankUtil.isBlank(clientInfos)){
					for(OccHouseClientInfoDO clientInfoDO : clientInfos){
						//拼装房号
						houseNumber.append(clientInfoDO.getRealHouseNumber()).append(",");
					}
					houseNumber = houseNumber.deleteCharAt(houseNumber.length()-1);
					perfectItemRequisitionVO.setHouseNumber(houseNumber.toString());
				}
			}
		}
		PerfectItemRequisitionDO itemRequisitionDO = this.assembleV2D(perfectItemRequisitionVO, null);
		itemRequisitionDO = perfectItemRequisitionDao.saveObject(itemRequisitionDO) ;
		perfectItemRequisitionVO.setKeyword(itemRequisitionDO.getKeyword());
		return perfectItemRequisitionVO;
	}
	/**
	 * 方法描述: 新增整改项目登记验证方法
	 * 必须成功办理入伙手续后，才能增加需整改项目
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return PerfectItemRequisitionDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkAddIsTrue(Long inspectionRequisitionId){
		//必须成功办理入伙手续后，才能增加需整改项目
		if(BlankUtil.isBlank(inspectionRequisitionId)){
			throw new BusinessException("occupation:occupation_perfectItemRequisition_add_exception", BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 修改整改项目登记验证方法
	 * 已经完工或派工的申报项目不得修改
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return PerfectItemRequisitionDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionDO checkEditIsTrue(Long perfectItemRequisitionId){
		Assert.notNull(perfectItemRequisitionId,"id isnot Null");
		PerfectItemRequisitionDO perfectItemRequisitionDO = perfectItemRequisitionDao.findById(perfectItemRequisitionId);
		//已经完工或派工的申报项目不得修改
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemRequisitionDO.getDispatchYn())){
			throw new BusinessException("occupation:occupation_perfectItemRequisition_edit_exception", BusinessException.WARNING);
		}
		return perfectItemRequisitionDO;
	}

	/**
	 * 方法描述: 修改整改项目登记方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemRequisitionVO
	 * @return PerfectItemRequisitionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionVO editPerfectItemRequisition(PerfectItemRequisitionVO perfectItemRequisitionVO){
		Assert.notNull(perfectItemRequisitionVO);
		PerfectItemRequisitionDO perfectItemRequisitionDO = this.checkEditIsTrue(perfectItemRequisitionVO.getKeyword());
		this.assembleV2D(perfectItemRequisitionVO, perfectItemRequisitionDO);
		perfectItemRequisitionDao.saveOrUpdateObject(perfectItemRequisitionDO);
		return perfectItemRequisitionVO;
	}
	/**
	 * 方法描述: 刪除整改项目登记验证方法
	 * 已经完工或派工的申报项目不得删除
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return PerfectItemRequisitionDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionDO checkDeleteIsTrue(Long perfectItemRequisitionId){
		Assert.notNull(perfectItemRequisitionId,"id isnot Null");
		PerfectItemRequisitionDO perfectItemRequisitionDO = perfectItemRequisitionDao.findById(perfectItemRequisitionId);
		//已经完工或派工的申报项目不得删除
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemRequisitionDO.getDispatchYn())){
			throw new BusinessException("occupation:occupation_perfectItemRequisition_delete_exception", BusinessException.WARNING);
		}
		return perfectItemRequisitionDO;
	}
	/**
	 * 方法描述: 删除整改项目登记方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemRequisitionId
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean deletePerfectItemRequisition(Long perfectItemRequisitionId){
		Assert.notNull(perfectItemRequisitionId,"id isnot Null");
		this.checkDeleteIsTrue(perfectItemRequisitionId);
		try {
			perfectItemRequisitionDao.deleteObjectById(perfectItemRequisitionId);
			return true;
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new BusinessException("occupation:system_delete_inused_exception", BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: PerfectItemRequisitionDO to PerfectItemRequisitionVO
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemRequisitionDO,perfectItemRequisitionVO
	 * @return PerfectItemRequisitionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private PerfectItemRequisitionVO assembleD2V(PerfectItemRequisitionDO perfectItemRequisitionDO,PerfectItemRequisitionVO perfectItemRequisitionVO){
		if(BlankUtil.isBlank(perfectItemRequisitionVO)){
			perfectItemRequisitionVO = new PerfectItemRequisitionVO();
		}
		PopulateUtil.populate(perfectItemRequisitionDO, perfectItemRequisitionVO);
		perfectItemRequisitionVO.setInspectionRequisitionId(perfectItemRequisitionDO.getInspectionRequisition().getKeyword());
		perfectItemRequisitionVO.setOwnerName(perfectItemRequisitionDO.getOwners());
		perfectItemRequisitionVO.setPerfectItemTypeConfigName(perfectItemRequisitionDO.getPerfectItemTypeConfig().getPerfectItemTypeName());
		perfectItemRequisitionVO.setPerfectItemTypeConfigId(perfectItemRequisitionDO.getPerfectItemTypeConfig().getKeyword());
		InspecteRequisiteDO inspecteRequisiteDO = perfectItemRequisitionDO.getInspectionRequisition();
		if(!BlankUtil.isBlank(inspecteRequisiteDO)){
			perfectItemRequisitionVO.setHouseId(inspecteRequisiteDO.getIdentityValidate().getHouse().getKeyword());
			perfectItemRequisitionVO.setBuildingName(inspecteRequisiteDO.getIdentityValidate().getHouse().getBuilding().getName());
			perfectItemRequisitionVO.setCheckResult(inspecteRequisiteDO.getCheckResult());
			Set<OccHouseClientInfoDO> occHouseClientDOSet = inspecteRequisiteDO.getIdentityValidate().getClientInfos();
			if(!BlankUtil.isBlank(occHouseClientDOSet)&&occHouseClientDOSet.size()>0){
				for(OccHouseClientInfoDO houseClientInfoDO : occHouseClientDOSet){
					perfectItemRequisitionVO.setCoalitionHouseNumber(houseClientInfoDO.getCoalitionHouseNumber());
					break;
				}
			}
		}
		//获取完工时间
		perfectItemRequisitionVO.setCompleteDate(perfectItemRequisitionDao.getCompleteDateById(perfectItemRequisitionDO.getKeyword()));
		SysrefcodeDO sysrefcodeDO = sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.OM_DISPATCH_STATE, perfectItemRequisitionDO.getDispatchYn());
		perfectItemRequisitionVO.setDispatchYnName(sysrefcodeDO==null?null:sysrefcodeDO.getName());
		PerfectItemDispatchDO dispatchDO = perfectItemRequisitionDO.getPerfectItemDispatch();
		perfectItemRequisitionVO.setPerfectItemDispatchId(dispatchDO==null?null:dispatchDO.getKeyword());
		return perfectItemRequisitionVO;
	}
	/**
	 * 方法描述:PerfectItemRequisitionVO to PerfectItemRequisitionDO
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemRequisitionVO,perfectItemRequisitionDO
	 * @return PerfectItemRequisitionDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private PerfectItemRequisitionDO assembleV2D(PerfectItemRequisitionVO perfectItemRequisitionVO,PerfectItemRequisitionDO perfectItemRequisitionDO){
		if(BlankUtil.isBlank(perfectItemRequisitionDO)){
			perfectItemRequisitionDO = new PerfectItemRequisitionDO();
		}
		BeanUtil.convertVoAsDo(perfectItemRequisitionVO, perfectItemRequisitionDO);
		perfectItemRequisitionDO.setOwners(perfectItemRequisitionVO.getOwnerName());
		perfectItemRequisitionDO.setInspectionRequisition(new InspecteRequisiteDO(perfectItemRequisitionVO.getInspectionRequisitionId()));
		if(!BlankUtil.isBlank(perfectItemRequisitionVO.getPerfectItemDispatchId())){
			perfectItemRequisitionDO.setPerfectItemDispatch(new PerfectItemDispatchDO(perfectItemRequisitionVO.getPerfectItemDispatchId()));
		}
		perfectItemRequisitionDO.setPerfectItemTypeConfig(new PerfectItemTypeConfigDO(perfectItemRequisitionVO.getPerfectItemTypeConfigId()));
		return perfectItemRequisitionDO;
	}
	/**
	 * 方法描述:DOlist to VOlist
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param DOlist
	 * @return List<PerfectItemRequisitionVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private List<PerfectItemRequisitionVO> changelistDOToVO(List<PerfectItemRequisitionDO> DOlist){
		if(DOlist.size() < 1){
			return null;
		}
		List<PerfectItemRequisitionVO> VOlist = new ArrayList<PerfectItemRequisitionVO>();
		for(PerfectItemRequisitionDO itemRequisitionDO : DOlist){
			VOlist.add(this.assembleD2V(itemRequisitionDO, null));
		}
		return VOlist;
	}
	/**
	 * 方法描述: 拼装房号和业主姓名（当入伙模式为管理房时，用逗号隔开）
	 * @author 胡煦
	 * Create on:2009-1-4
	 * @param perfectItemRequisitionVO
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionVO uniteHouseNOAndOwnerName(PerfectItemRequisitionVO perfectItemRequisitionVO){
		Map<String, List<String>> map = perfectItemRequisitionVO.getHouseNoAndOwnersMap();
		if(!BlankUtil.isBlank(map)){
			if(map.size() > 0){
				StringBuilder houseNumber = new StringBuilder("");
				StringBuilder ownerName = new StringBuilder("");
				for(String houseNo : map.keySet()){
					// 拼装房号
					houseNumber.append(houseNo).append(",");
					List<String> list = map.get(houseNo);
					if(list.size()>0){
						// 拼装业主名字
						for(String str : list)
						{
							ownerName.append(str).append(",");
						}
					}
				}
				houseNumber = houseNumber.deleteCharAt(houseNumber.length()-1);
				ownerName = ownerName.deleteCharAt(ownerName.length()-1);
				perfectItemRequisitionVO.setHouseNumber(houseNumber.toString());
				perfectItemRequisitionVO.setOwnerName(ownerName.toString());
			}
			return perfectItemRequisitionVO;
		}
		return null;
	}

	/**
	 * @param identityValidateDao the identityValidateDao to set
	 */
	public void setIdentityValidateDao(IIdentityValidateDao identityValidateDao) {
		this.identityValidateDao = identityValidateDao;
	}

	private IPerfectItemRequisitionDao perfectItemRequisitionDao;
	private IPerfectItemDispatchDao perfectItemDispatchDao;
	private IIdentityValidateDao identityValidateDao;
	/**
	 * @param perfectItemRequisitionDao the perfectItemRequisitionDao to set
	 */
	public void setPerfectItemRequisitionDao(
			IPerfectItemRequisitionDao perfectItemRequisitionDao) {
		this.perfectItemRequisitionDao = perfectItemRequisitionDao;
	}
	/**
	 * @param perfectItemDispatchDao the perfectItemDispatchDao to set
	 */
	public void setPerfectItemDispatchDao(
			IPerfectItemDispatchDao perfectItemDispatchDao) {
		this.perfectItemDispatchDao = perfectItemDispatchDao;
	}
}
